import {
  getPredictionData,
  getStatisticsData,
  getCoreDataTrend,
  getContentChangeData,
  getFuelTrackingData,
  getScoreQuntile,
  getAutoScore,
  getCurrentData
} from '@/services/hanfan';
import time, {currentTime} from "@/utils/time";


const initState = {
  predictionData: null,
  contentStatistics: null,
  coreDataTrend: null,
  globalTime: {
    startOfDay: time.getStartOfDay(),
    endOfDay: time.getEndOfDay(),
    currentTime: currentTime()
  },
  autoScore:{}
};

const HanFan = {
  namespace: 'hanfan',
  state: initState,
  effects: {
    // 异步方法,应该通常用于前后端交互
    * predictionData({payload}, {call, put}) {
      const response = yield call(getPredictionData, payload);
      // dispatch转发action
      yield put({
        type: 'saveValues',
        payload: response.detail,
        name: 'predictionData'
      });
    },
    * currentData({payload}, {call, put}) {
      const response = yield call(getCurrentData, payload);
      // dispatch转发action
      yield put({
        type: 'saveValues',
        payload: response.detail,
        name: 'currentData'
      });
    },
    * contentStatistics({payload}, {call, put}) {
      const response = yield call(getStatisticsData, payload);
      // dispatch转发action
      yield put({
        type: 'saveValues',
        payload: response.detail,
        name: 'contentStatistics'
      });
    },
    * coreDataTrend({payload}, {call, put}) {
      const response = yield call(getCoreDataTrend, payload);
      // dispatch转发action
      yield put({
        type: 'saveValues',
        payload: response.detail,
        name: 'coreDataTrend'
      });
    },
    * contentChange({payload}, {call, put}) {
      const response = yield call(getContentChangeData, payload);
      // dispatch转发action
      yield put({
        type: 'saveValues',
        payload: response.detail,
        name: 'contentChange'
      });
    },
    * fuelTracking({payload}, {call, put}) {
      const response = yield call(getFuelTrackingData, payload);
      // dispatch转发action
      yield put({
        type: 'saveValues',
        payload: response.detail,
        name: 'fuelTracking'
      });
    },
    * getScoreQuntile({payload}, {call, put}) {
      const response = yield call(getScoreQuntile, payload);
      // dispatch转发action
      yield put({
        type: 'saveValues',
        payload: response.detail,
        name: 'scoreQuntile'
      });
    },
    * autoScore({payload}, {call, put}) {
      const response = yield call(getAutoScore, payload);
      // dispatch转发action
      yield put({
        type: 'saveValues',
        payload: response.detail,
        name: 'autoScore'
      });
    },
  },
  reducers: {
    saveValues(state, {payload, name}) {
      const v = {...state};
      v[name] = payload;
      return v
    },
  },
};
export default HanFan;
